﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using GameServer.Virtual_Objects.Room;
using GameServer.Virtual_Objects.User;
using GameServer.Managers;
using GameServer.Networking.Packets;

namespace GameServer.Networking.Handlers
{
    class HANDLE_ROOM_CREATION : PacketHandler
    {
        public override void Handle(GameServer.Virtual_Objects.User.virtualUser User)
        {
            try
            {
                int ResultID = RoomManager.getOpenID(User.Channel);

                if (User.Channel != 4)
                {
                    if (ResultID >= 0 && RankManager.HasPermision(User.Rank, "account.createroom") && (User.Channel >= 1 && User.Channel <= 5))
                    {
                        if (User.Room == null)
                        {
                            virtualRoom Room = new virtualRoom();

                            /*ToXiiC's Noob Stuff*/
                            Room.ID = ResultID;
                            Room.Channel = User.Channel;
                            Room.Name = getNextBlock();
                            Room.EnablePassword = int.Parse(getNextBlock());
                            Room.Password = getNextBlock();
                            Room.MaxPlayers = (int.Parse(getNextBlock()) + 1);
                            Room.MapID = int.Parse(getNextBlock());
                            Room.SuperMaster = User.hasItem("CC02");

                            getNextBlock(); getNextBlock();               

                            if (Room.Name.StartsWith("E|") && User.Rank > 2 && Room.Name.Length > 2)
                            {
                                Room.Name = Room.Name.Substring(2);
                                Room.RoomType = 3;
                                getNextBlock();
                            }
                            else if (Room.Name.Contains("CW|") && User.ClanID != -1 && Room.Name.Length > 3 && User.Channel == 1)
                            {
                                Room.Name = Room.Name.Substring(3);
                                Room.RoomType = 1;
                                getNextBlock();
                            }
                            else
                            {
                                Room.RoomType = int.Parse(getNextBlock());
                            }
                            Room.LevelLimit = int.Parse(getNextBlock());
                            Room.PremiumOnly = int.Parse(getNextBlock());
                            Room.VoteKick = int.Parse(getNextBlock());
                            Room.TimeLimit = 3;
                            if (User.Premium == 0 && Room.PremiumOnly > 0)
                                Room.PremiumOnly = 0;

                            switch (Room.Channel)
                            {
                                case 1:
                                    {
                                        if (User.Premium > 0)
                                        {
                                            Room.Mode = 2;
                                            Room.MapID = 27;
                                            Room.Rounds = 2;
                                        }
                                        else
                                        {
                                            Room.Mode = 0;
                                            Room.MapID = 12;
                                            Room.Rounds = 3;
                                        }
                                        Room.MaxPlayers *= 8;
                                        break;
                                    }
                                case 2:
                                    {
                                        Room.Mode = 2;
                                        Room.TimeLimit = 3;
                                        Room.MapID = 1;
                                        switch (Room.MaxPlayers)
                                        {
                                            case 1: Room.MaxPlayers = 8; break;
                                            case 2: Room.MaxPlayers = 16; break;
                                            case 3: Room.MaxPlayers = 20; break;
                                            case 4: Room.MaxPlayers = 24; break;
                                        }
                                        break;
                                    }
                                case 3:
                                    {
                                        Room.Mode = 2;
                                        Room.TimeLimit = 4;
                                        Room.MapID = 2;
                                        switch (Room.MaxPlayers)
                                        {
                                            case 1: Room.MaxPlayers = 8; break;
                                            case 2: Room.MaxPlayers = 16; break;
                                            case 3: Room.MaxPlayers = 20; break;
                                            case 4: Room.MaxPlayers = 24; break;
                                            case 5: Room.MaxPlayers = 32; break;
                                        }
                                        break;
                                    }
                            }

                            if (Room.joinUser(User))
                            {
                                if (RoomManager.addRoom(Room.Channel, Room.ID, Room))
                                {
                                    //if(Room.RoomType == 1) Room.Name = User.ClanName + " vs. None";
                                    User.isReady = false;
                                    User.isSpawned = false;
                                    User.send(new PACKET_CREATE_ROOM(Room));

                                    foreach (Virtual_Objects.User.virtualUser _User in UserManager.getUsersInChannel(Room.Channel, false))
                                    {
                                        if (_User.Page == Math.Floor(Convert.ToDecimal(Room.ID / 14)))
                                        {
                                            _User.send(new PACKET_ROOM_UPDATE(Room, 0));
                                            _User.send(new PACKET_ROOM_UPDATE(Room));

                                        }
                                    }
                                }
                                else
                                {
                                    Log.WriteError("Couldn't Add Room" + Room.ID + ":" + Room.Name + " To the Room Pool");
                                    //Error Joining Room?
                                    User.disconnect();
                                }
                            }
                            else
                            {
                                Log.WriteError("Couldn't Join Room" + Room.ID + ":" + Room.Name + " As Master");
                                //Error Joining Room?
                                User.disconnect();
                            }
                        }
                    }
                }
                else
                {
                    if (ResultID >= 0 && RankManager.HasPermision(User.Rank, "account.createroom") && (User.Channel >= 1 && User.Channel <= 5))
                    {
                        if (User.Room == null)
                        {
                            virtualRoom Room = new virtualRoom()
                            {
                                ID = ResultID,
                                Channel = User.Channel,
                                Name = getNextBlock(),
                                EnablePassword = int.Parse(getNextBlock()),
                                Password = getNextBlock(),
                                MaxPlayers = 4,
                                MapID = 46,
                                SuperMaster = User.hasItem("CC02")
                            };

                            Room.Mode = 7;
                            Room.MapID = 46;

                            if (Room.joinUser(User))
                            {
                                if (RoomManager.addRoom(Room.Channel, Room.ID, Room))
                                {
                                    User.send(new PACKET_CREATE_ROOM(Room));

                                    foreach (Virtual_Objects.User.virtualUser _User in UserManager.getUsersInChannel(Room.Channel, false))
                                    {
                                        if (_User.Page == Math.Floor(Convert.ToDecimal(Room.ID / 14)))
                                        {
                                            _User.send(new PACKET_ROOM_UPDATE(Room, 0));
                                            _User.send(new PACKET_ROOM_UPDATE(Room));
                                        }
                                    }
                                }
                                else
                                {
                                    Log.WriteError("Couldn't Add Room" + Room.ID + ":" + Room.Name + " To the Room Pool");
                                    //Error Joining Room?
                                    User.disconnect();
                                }
                            }
                            else
                            {
                                Log.WriteError("Couldn't Join Room" + Room.ID + ":" + Room.Name + " As Master");
                                //Error Joining Room?
                                User.disconnect();
                            }
                        }
                    }
                }
            }
            catch (Exception ex) { Log.WriteError(ex.Message); }
        }
    }
}